<template>
  <div class="zuo">
    <div id="driver">
      <van-nav-bar
          title=""
          left-arrow
          @click-left="onClickLeft"
      />
    <h3>添加车辆信息</h3>

    <el-form ref="form" :model="form" label-width="80px">
      <el-upload
          class="avatar-uploader"
          action="order/imageUpload/files"
          :show-file-list="false"
          name="file"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width: 200px;height: 200px">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      <el-form-item label="车辆类型">
        <el-select v-model="form.vechicleType" placeholder="请选择活动区域">
          <el-option label="轿车" value="1"></el-option>
          <el-option label="汽车" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号">
        <el-input @blur="span1" v-model="form.licensePlateNumber"></el-input>
        <span v-if="span1error" style="color: red">车牌号不能为空</span>
      </el-form-item>
      <el-form-item label="车型">
        <el-select v-model="form.vehicleModel" placeholder="请选择活动区域">
          <el-option label="宝来" value="1"></el-option>
          <el-option label="宝马" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="能源类型">
        <el-radio-group v-model="form.energyType">
          <el-radio label="油电混合"></el-radio>
          <el-radio label="天然气"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="品牌">
        <el-input v-model="form.brand"></el-input>
      </el-form-item>
      <el-form-item label="厂家">
        <el-input v-model="form.factory"></el-input>
      </el-form-item>
      <el-form-item label="车辆VIN编号">
        <el-input v-model="form.vinNumber"></el-input>

      </el-form-item>
      <el-form-item label="发动机编号">
        <el-input v-model="form.engineNumber"></el-input>

      </el-form-item>
      <el-form-item label="电池容量">
        <el-input v-model="form.batteryCapacity" placeholder="容量单位Ah"></el-input>
      </el-form-item>
      <el-form-item label="车龄">
        <el-input v-model="form.cheage" placeholder="年"></el-input>
      </el-form-item>
      <el-form-item label="CTLC续航里程数">
        <el-input v-model="form.endurance" placeholder="公里"></el-input>
      </el-form-item>
      <el-form-item label="电池类型">
        <el-radio-group v-model="form.batterytype">
          <el-radio label="锂电池"></el-radio>
          <el-radio label="汞电池"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="quxiao">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      form: {
        vechicleType:'',
        licensePlateNumber:'',
        batteryCapacity:'',
        brand:'',
        vehicleModel:'',
        energyType:'',
        factory:'',
        vinNumber:'',
        engineNumber:'',
        cheage:'',
        endurance:'',
        batterytype:'',
      },
      did:'',
      imageUrl: '',

    };
  },
  created() {
    this.findList()
  },
  methods: {
    onClickLeft(){
      this.$router.push('/PersonalCenter')
    },
    quxiao(){
      this.form = {}
    },
    onSubmit() {
      if (this.form.licensePlateNumber===''){
        alert('车牌号不能为空')
      }else if (this.form.brand===''){
        alert('品牌不能为空')
      }else if (this.form.vinNumber===''){
        alert('车辆VIN编号不能为空')
      }else if (this.form.engineNumber===''){
        alert('发动机编号不能为空')
      }else if (this.form.cheage===''){
        alert('车龄不能为空')
      }else{
        this.form.did=this.did
        // this.form.picturesimage=this.imageUrl
        console.log(this.imageUrl)
        console.log('这是imageUrl')
        axios.post(`order/vehicle/addCar`, this.form).then(res => {
          console.log(res)
          this.form = {}
        })
      }


    },
    findList() {
      const tel = JSON.parse(localStorage.getItem('login'));
      console.log(tel)
      console.log('手机号')
      axios.get(`order/driver/findOne?tel=${tel}`).then(res => {
        this.clist = res.data
        this.did = this.clist.id
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }

};
</script>

<style scoped>

#driver{
  width: 95%;
}
h3{
  text-align: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
